﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Data;
using System.Windows.Forms;

namespace QuickStart
{
    class StartInfo
    {
        private static StartInfo instance;
        //private XmlDocument xmlDoc;

        private XDocument xDoc;

        const string SETTING_FILE = "Program.xml";

        private StartInfo()
        {
            //xmlDoc = new XmlDocument();


            if (File.Exists(SETTING_FILE))
            {
                //xmlDoc.Load(SETTING_FILE);
                xDoc= XDocument.Parse(File.ReadAllText(SETTING_FILE));
            }
            else
            {
                //xmlDoc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\" ?><programs />");
                xDoc = XDocument.Parse("<?xml version=\"1.0\" encoding=\"utf-8\" ?><programs />");
            }
        }

        public static StartInfo GetInstance()
        {
            if (instance == null)
            {
                instance = new StartInfo();               
            }

            return instance;
        }


        public void Insert(string shortcut, string path)
        {

            XElement xe = new XElement("program",
                    new XAttribute("shortcut", shortcut),
                    new XAttribute("path", path));

            xDoc.Root.Add(xe);

            Save();
        }

        public void Modify(string originShortcut, string shortcut, string path)
        {

            var elems = xDoc.Root.DescendantNodes();

            var xe = from XElement elem in elems
                     where String.Compare(elem.FirstAttribute.Value, originShortcut, true) == 0
                     select elem;

            foreach (XElement elem in xe)
            {
                XAttribute xa = elem.FirstAttribute;
                xa.Value = shortcut;
                xa = xa.NextAttribute;
                xa.Value = path;
            }

            Save();
        }

        public void Delete(string shortcut)
        {

            var elems = xDoc.Root.DescendantNodes();

            var xe = from XElement elem in elems
                     where String.Compare(elem.FirstAttribute.Value, shortcut, true) == 0
                     //select elem.FirstAttribute.NextAttribute.Value;
                     select elem;

            for (int i = xe.Count() - 1; i >= 0; i-- )
            {
                xe.ElementAt(i).Remove();
            }

            Save();
        }

        public bool Validate(string shortcut, string path)
        {
            var elems = xDoc.Root.DescendantNodes();
            var xe = from XElement elem in elems
                     where Exists(elem,shortcut,path)
                     select elem;

            return xe.Count() == 0;
        }

        private bool Exists(XElement elem, string shortcut, string path)
        {
            XAttribute shortcutAtt = elem.FirstAttribute;
            XAttribute pathAtt = shortcutAtt.NextAttribute;

            if (String.Compare(shortcutAtt.Value, shortcut, true) == 0 )
                return true;
            else
                return false;
        }

        public XDocument getXDocument()
        {
            return instance.xDoc;
        }

        private void Save()
        {
            StreamWriter sw = File.CreateText(SETTING_FILE);
            sw.Write(xDoc.ToString());
            sw.Close();
        }

        public void Run(string shortcut)
        {
            var elems = xDoc.Root.DescendantNodes();

            var xe = from XElement elem in elems
                     where String.Compare(elem.FirstAttribute.Value, shortcut, true) == 0
                     select elem.FirstAttribute.NextAttribute.Value;

            if (xe.Count() > 0)
            {
                foreach (string path in xe)
                {
                    RunApp(path);
                }
            }
            else
            {
                RunApp(shortcut);
            }
        }

        private void RunApp(string path)
        {
            try
            {
                System.Diagnostics.Process.Start(path);
            }
            catch (Exception)
            {
                
            }
        }


        public DataView ToDataView(string orderField="")
        {
            
            var elems = xDoc.Root.DescendantNodes();

            var xe = from XElement elem in elems
                     //where String.Compare(elem.FirstAttribute.Value, shortcut, true) == 0
                     select elem.FirstAttribute.NextAttribute.Value;

            DataTable table;
            DataView view;
                
            table = new DataTable();
            table.Columns.Add(new DataColumn("Shortcut",typeof(String)));
            table.Columns.Add(new DataColumn("Path",typeof(String)));

            foreach (XElement elem in elems)
            {
                DataRow dr = table.NewRow();
                dr[0] = elem.FirstAttribute.Value;
                dr[1] =elem.FirstAttribute.NextAttribute.Value;
                table.Rows.Add(dr);
            }

            view = table.DefaultView;

            //if (orderField != "")
            //{
                view.Sort = orderField;
            //}

            return view;
        }
    }
}
